<template>
  <el-card>
    <BreadCrumb />
    <el-form ref="FormRef" :model="form" :rules="FormRules" label-width="120px">
      <el-form-item
        label="专业名称"
        prop="majorName"
      >
        <el-input v-model="form.majorName" autocomplete="off" />
      </el-form-item>
      <el-form-item
        prop="sort"
        label="排序序号"
      >
        <el-input
          v-model="form.sort"
          autocomplete="off"
          placeholder="数字越大，排序越靠后"
        />
      </el-form-item>

      <el-form-item class="form_button">
        <el-button type="primary" @click="addMajor()">
          添加
        </el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import BreadCrumb from '@/components/BreadCrumb'
import { isConfirm } from '@/utils/confirm'
import { addMajor } from '@/api/majorApi'
export default {
  components: {
    BreadCrumb
  },
  data() {
    return {
      // 专业表单数据
      form: {
        sort: '',
        majorName: ''
      },
      FormRules: {
        majorName: [{ required: true, message: '请输入专业名称', trigger: 'blur' }],
        sort: [{ required: true, message: '请输入排序序号', trigger: 'blur' }]
      }
    }
  },
  created() {

  },
  methods: {
    // 添加专业
    addMajor() {
      // 表单预校验
      this.$refs.FormRef.validate(async valid => {
        if (!valid) return
        // 确认添加
        if (await isConfirm('添加后将无法删除，是否继续？')) {
          // 发送请求，添加专业
          this.form.sort = Number(this.form.sort)
          await addMajor(this.form)
          this.$message.success('添加成功')
          // 清空表单
          this.$refs.FormRef.resetFields()
        }
      })
    }
  }
}
</script>

<style scoped>
.el-input {
  width: 400px;
}
.el-form-item {
  margin: 0 0 50px -30px;
}
</style>
